//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "VolatilityInterpolationSpecifierabcd.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/Models/PiecewiseConstantVariance.h>
#include <gen/QL/Models/Marketmodels/Models/PiecewiseConstantAbcdVariance.h>
#include <gen/QL/Models/Marketmodels/Models/VolatilityInterpolationSpecifier.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (UInt64 period, UInt64 offset, Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantAbcdVariance^>^ originalVariances, Cephei::IVector<Double>^ timesForSmallRates, Microsoft::FSharp::Core::FSharpOption<Double>^ lastCapletVol_) : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
    CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantAbcdVariance^>^ _CoriginalVariances;
    CoVector<Double>^ _CtimesForSmallRates;
    try
    {
#ifdef HANDLE
        _phVolatilityInterpolationSpecifierabcd = NULL;
#endif
        QuantLib::Size _period = (QuantLib::Size)ValueHelper::Convert (period);
        QuantLib::Size _offset = (QuantLib::Size)ValueHelper::Convert (offset);
        _CoriginalVariances = safe_cast<CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantAbcdVariance^>^> (originalVariances);
        _CoriginalVariances ->Lock ();
        INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantAbcdVariance^>^ _NCIoriginalVariances = _CoriginalVariances->getFeature (NativeFeature::Value);
        CPiecewiseConstantAbcdVarianceVector^ _NCoriginalVariances = safe_cast<CPiecewiseConstantAbcdVarianceVector^>(_NCIoriginalVariances);
        std::vector< QuantLib::PiecewiseConstantAbcdVariance>& _originalVariances = static_cast<std::vector< QuantLib::PiecewiseConstantAbcdVariance>&> (_NCoriginalVariances->GetReference ());
        CoVector<Double>^ _CtimesForSmallRates = safe_cast<CoVector<Double>^> (timesForSmallRates);
        _CtimesForSmallRates->Lock();
        INativeVector<Double>^ _NCItimesForSmallRates = _CtimesForSmallRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtimesForSmallRates = safe_cast<CDoubleVector^>(_NCItimesForSmallRates);
        std::vector<QuantLib::Time>& _timesForSmallRates = static_cast<std::vector<QuantLib::Time>&> (_NCtimesForSmallRates->GetReference ());
        QuantLib::Real _lastCapletVol_ = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (lastCapletVol_) ? (QuantLib::Real)ValueHelper::Convert (lastCapletVol_->Value) : 0.0); //4
        _ppVolatilityInterpolationSpecifierabcd = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd> (new QuantLib::VolatilityInterpolationSpecifierabcd ( _period,  _offset,  _originalVariances,  _timesForSmallRates,  _lastCapletVol_ ));
        SetVolatilityInterpolationSpecifier (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoriginalVariances != nullptr) _CoriginalVariances->Unlock();
        if (_CtimesForSmallRates != nullptr) _CtimesForSmallRates->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd>& childNative, Object^ owner) : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifierabcd = NULL;
#endif
	_ppVolatilityInterpolationSpecifierabcd = &childNative;
    _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (QuantLib::VolatilityInterpolationSpecifierabcd& childNative, Object^ owner) : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifierabcd = NULL;
#endif
	_ppVolatilityInterpolationSpecifierabcd = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd> (&childNative);
    _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
    _VolatilityInterpolationSpecifierabcdOwner = owner;
    _VolatilityInterpolationSpecifierOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (CVolatilityInterpolationSpecifierabcd^ copy) : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifierabcd = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVolatilityInterpolationSpecifierabcd = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd> (copy->GetShared());
        _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
    }
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (System::Type^ t) : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifierabcd = NULL;
#endif
	if (!t->IsSubclassOf(CVolatilityInterpolationSpecifierabcd::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifierabcd>& childNative, Object^ owner)  : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
	_phVolatilityInterpolationSpecifierabcd = &childNative;
	_ppVolatilityInterpolationSpecifierabcd = &static_cast<boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd>>(childNative.currentLink());
    _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
    _VolatilityInterpolationSpecifierabcdOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifierabcd> childNative)  : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
	_phVolatilityInterpolationSpecifierabcd = &childNative;
	_ppVolatilityInterpolationSpecifierabcd = &static_cast<boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd>>(childNative.currentLink());
    _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::CVolatilityInterpolationSpecifierabcd (QuantLib::VolatilityInterpolationSpecifierabcd childNative)  : CVolatilityInterpolationSpecifier(CVolatilityInterpolationSpecifierabcd::typeid)
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifierabcd = NULL;
#endif
	_ppVolatilityInterpolationSpecifierabcd = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd> (new QuantLib::VolatilityInterpolationSpecifierabcd (childNative));
    _ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (boost::dynamic_pointer_cast<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifierabcd));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::~CVolatilityInterpolationSpecifierabcd ()
{
    if (_ppVolatilityInterpolationSpecifierabcd != NULL)
    {
	    delete _ppVolatilityInterpolationSpecifierabcd;
        _ppVolatilityInterpolationSpecifierabcd = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::!CVolatilityInterpolationSpecifierabcd ()
{
    if (_ppVolatilityInterpolationSpecifierabcd != NULL)
    {
	    delete _ppVolatilityInterpolationSpecifierabcd;
    }
}
QuantLib::VolatilityInterpolationSpecifierabcd& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetReference ()
{
    if (_ppVolatilityInterpolationSpecifierabcd == NULL) throw gcnew NativeNullException ();
	return **_ppVolatilityInterpolationSpecifierabcd;
}
boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifierabcd>& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetShared ()
{
    if (_ppVolatilityInterpolationSpecifierabcd == NULL) throw gcnew NativeNullException ();
	return *_ppVolatilityInterpolationSpecifierabcd;
}
QuantLib::VolatilityInterpolationSpecifierabcd* Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetPointer ()
{
    if (_ppVolatilityInterpolationSpecifierabcd == NULL) throw gcnew NativeNullException ();
	return &**_ppVolatilityInterpolationSpecifierabcd;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifierabcd>& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetHandle ()
{
	if (_phVolatilityInterpolationSpecifierabcd == NULL)
	{
		_phVolatilityInterpolationSpecifierabcd = new Handle<QuantLib::VolatilityInterpolationSpecifierabcd> (*_ppVolatilityInterpolationSpecifierabcd);
	}
	return *_phVolatilityInterpolationSpecifierabcd;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::HasNative () 
{
	return (_ppVolatilityInterpolationSpecifierabcd != NULL);
}

UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetNoBigRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifierabcd)->getNoBigRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetNoSmallRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifierabcd)->getNoSmallRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetOffset::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifierabcd)->getOffset ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::GetPeriod::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifierabcd)->getPeriod ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::InterpolatedVariances::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >& _rv = (std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >&)(*_ppVolatilityInterpolationSpecifierabcd)->interpolatedVariances ( );   
        CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _nrv = gcnew CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>(gcnew CPiecewiseConstantVarianceVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::OriginalVariances::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >& _rv = (std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >&)(*_ppVolatilityInterpolationSpecifierabcd)->originalVariances ( );   
        CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _nrv = gcnew CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>(gcnew CPiecewiseConstantVarianceVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Models::IVolatilityInterpolationSpecifierabcd^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::SetLastCapletVol (Double vol)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _vol = (QuantLib::Real)ValueHelper::Convert (vol);
    	(*_ppVolatilityInterpolationSpecifierabcd)->setLastCapletVol ( _vol );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Models::IVolatilityInterpolationSpecifierabcd^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd::SetScalingFactors (Cephei::IVector<Double>^ scales)
{
    CoVector<Double>^ _Cscales;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cscales = safe_cast<CoVector<Double>^> (scales);
        _Cscales->Lock();
        INativeVector<Double>^ _NCIscales = _Cscales->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCscales = safe_cast<CDoubleVector^>(_NCIscales);
        std::vector<QuantLib::Real>& _scales = static_cast<std::vector<QuantLib::Real>&> (_NCscales->GetReference ());
    	(*_ppVolatilityInterpolationSpecifierabcd)->setScalingFactors ( _scales );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cscales != nullptr) _Cscales->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IVolatilityInterpolationSpecifierabcd^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifierabcd_Factory::Create (UInt64 period, UInt64 offset, Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantAbcdVariance^>^ originalVariances, Cephei::IVector<Double>^ timesForSmallRates, Microsoft::FSharp::Core::FSharpOption<Double>^ lastCapletVol_)
{
    return gcnew CVolatilityInterpolationSpecifierabcd ( period,  offset,  originalVariances,  timesForSmallRates,  lastCapletVol_);
}
